# Replication archive for:
# Galos and Coppock. 2023. 
# Gender Composition Predicts Gender Bias: A Meta-reanalysis of Hiring Discrimination Audit Experiments. 
# Science Advances. Forthcoming.

library(tidyverse)
library(metafor)
library(broom)
source("code/helpers.R")

gender_gradients_df <- read_rds("data/gender_gradients_df.rds")

gender_gradients_meta_fit <-
  rma.uni(
    yi = estimate,
    sei = std.error,
    method = "REML",
    data = gender_gradients_df
  )

gender_gradients_df$weight <- weights(gender_gradients_meta_fit)

gender_gradients_meta_ests <- 
  gender_gradients_meta_fit |> 
  tidy(conf.int = TRUE) |> 
  mutate(study_id = "Meta-analysis",
         study_name = "Meta-analysis",
         facet = "Meta",
         weight = mean(gender_gradients_df$weight))


# Join study level with meta analysis -----------------------

gender_gradients_df |> 
  mutate(positive = estimate > 0,
         significant = p.value <= 0.05) |> 
  group_by(positive, significant) |> 
  summarise(n = n(),
            frac = n / nrow(gender_gradients_df))

gg_gender_gradients <- 
  bind_rows(gender_gradients_df, gender_gradients_meta_ests) %>% 
  ungroup() %>% 
  mutate(study_name_fct = fct_reorder(study_id, estimate),
         entry = make_se_entry(estimate, std.error, 1),
         facet = factor(facet, levels = c("Study gender gradient estimates", "Meta")))

g1 <- 
  ggplot(gg_gender_gradients, aes(estimate, study_name_fct)) +
  geom_point(aes(size = weight)) +
  geom_text(x = -80, aes(label = entry), hjust = 1, size = 2.5) +
  geom_linerange(aes(xmin = conf.low, xmax = conf.high), alpha = 0.3) +
  geom_vline(xintercept = 0, alpha = 0.25, linetype = "dashed") +
  geom_vline(data = select(gender_gradients_meta_ests, -facet), aes(xintercept = estimate)) +
  geom_rect(
    data = select(gender_gradients_meta_ests, -facet),
    aes(
      xmin = conf.low,
      xmax = conf.high,
      x = NULL,
      y = NULL
    ),
    ymin = 0,
    ymax = 100,
    alpha = .1
  ) +
  scale_size_area(max_size = 3) +
  theme_bw() +
  facet_grid(rows = vars(facet), scales = "free_y", space = "free") +
  coord_cartesian(xlim = c(-100, 100)) +
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        panel.grid = element_line(color = gray(0.95)),
        panel.grid.minor = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_text(size = 5),
        legend.position = "none") +
  labs(x = "Estimated gender gradient")

g1
